﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LockCrypt.Core;

namespace LockCrypt {
    public class FirefoxTest {
        public static void TestAll() {
            FirefoxTest test = new FirefoxTest();
            test.PartialDomainWildcard();
            test.MultipleWildcards();
            test.NoWildcard();
            
            test.ProtocolDomainPathWildcard();
            test.ProtocolDomainWildcard();
            test.ProtocolWildcard();
        }

        public void NoWildcard() {
            Dictionary<string, bool> results = new Dictionary<string, bool>()
                                                   {
                                                       {"http://www.google.com/", true},
                                                       {"http://google.com/", false},
                                                       {"http://www.google.co.za/", false}
                                                   };
            WildcardUrl urlInAccount = new WildcardUrl("http://www.google.com/");
            foreach(var test in results) {
                bool matches = urlInAccount.Matches(test.Key);
                Console.WriteLine("{0} {1}match {2} - {3}", urlInAccount, test.Value ? string.Empty : "does not ", test.Key, test.Value == matches ? "Pass": "Fail");
            }
        }

        public void PartialDomainWildcard() {
            Dictionary<string, bool> results = new Dictionary<string, bool>()
                                                   {
                                                       {"http://groups.google.com/group1", true},
                                                       {"http://www.google.com/", true},
                                                       {"http://groups.google.com/", true},
                                                       {"http://google.jp/", false},
                                                       {"http://groups.google.jp/", false},
                                                   };
            WildcardUrl urlInAccount = new WildcardUrl("http://*.google.com/");
            foreach(var test in results) {
                bool matches = urlInAccount.Matches(test.Key);
                Console.WriteLine("{0} {1}match {2} - {3}", urlInAccount, test.Value ? string.Empty : "does not ", test.Key, test.Value == matches ? "Pass" : "Fail");
            }
        }


        public void ProtocolWildcard() {
            Dictionary<string, bool> results = new Dictionary<string, bool>()
                                                   {
                                                       {"gopher://mycompany/login/", true},
                                                       {"http://mycompany/search.aspx", true},
                                                       {"http://google.com/", false},
                                                       {"http://ftp.microsoft.com/", false},
                                                   };
            WildcardUrl urlInAccount = new WildcardUrl("*://mycompany/");
            foreach(var test in results) {
                bool matches = urlInAccount.Matches(test.Key);
                Console.WriteLine("{0} {1}match {2} - {3}", urlInAccount, test.Value ? string.Empty : "does not ", test.Key, test.Value == matches ? "Pass" : "Fail");
            }
        }

        public void ProtocolDomainWildcard() {

            Dictionary<string, bool> results = new Dictionary<string, bool>()
                                                   {
                                                       {"ftp://ftp.yahoo.com", true},
                                                       {"http://www.microsoft.com/", true},
                                                       {"http://google.com/", true},
                                                       {"http://google.net/", false},
                                                   };
            WildcardUrl urlInAccount = new WildcardUrl("*://*.com");
            foreach(var test in results) {
                bool matches = urlInAccount.Matches(test.Key);
                Console.WriteLine("{0} {1}match {2} - {3}", urlInAccount, test.Value ? string.Empty : "does not ", test.Key, test.Value == matches ? "Pass" : "Fail");
            }
        }

        public void MultipleWildcards() {
            Dictionary<string, bool> results = new Dictionary<string, bool>()
                                                   {
                                                       {"http://intra.contractor/login.php", false},
                                                       {"http://google.com/login.php", false},
                                                       {"http://google.fr/admin/login.php", false},
                                                       {"http://intra.google.com/login.php", true},
                                                   };
            WildcardUrl urlInAccount = new WildcardUrl("http://*intra.google*/*login.php");
            foreach(var test in results) {
                bool matches = urlInAccount.Matches(test.Key);
                Console.WriteLine("{0} {1}match {2} - {3}", urlInAccount, test.Value ? string.Empty : "does not ", test.Key, test.Value == matches ? "Pass" : "Fail");
            }
        }

        public void ProtocolDomainPathWildcard() {
            Dictionary<string, bool> results = new Dictionary<string, bool>()
                                                   {
                                                       {"http://www.microsoft.com/login.php", true},
                                                       {"http://yahoo.com/login.php", true},
                                                       {"http://google.com/search.php", false},
                                                   };
            WildcardUrl urlInAccount = new WildcardUrl("*login.php");
            foreach(var test in results) {
                bool matches = urlInAccount.Matches(test.Key);
                Console.WriteLine("{0} {1}match {2} - {3}", urlInAccount, test.Value ? string.Empty : "does not ", test.Key, test.Value == matches ? "Pass" : "Fail");
            }
        }
    }
}